package com.atguigu.spzx.common.interceptor;

import com.atguigu.spzx.common.config.SpzxAuthWhiteUrls;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SpzxWebMvcConfigurer implements WebMvcConfigurer {
    @Resource
    SpzxLoginAuthInterceptor spzxLoginAuthInterceptor;
    @Resource
    SpzxAuthWhiteUrls spzxAuthWhiteUrls;
    //全局跨域配置

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //服务器中哪些资源需要跨域访问检查
                .allowedOriginPatterns("*") //允许哪些前端服务器跨域访问
                .allowCredentials(true) //允许携带cookie
                .allowedMethods("*")
                .allowedHeaders("*");
    }

    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(spzxLoginAuthInterceptor)
               //拦截路径的规则： /** 拦截所有
               .addPathPatterns("/**")
               .excludePathPatterns(spzxAuthWhiteUrls.getUrls());
    }
}
